from django.contrib import admin
from django.utils.safestring import mark_safe
from . import models


@admin.register(models.Book)
class BookAdmin(admin.ModelAdmin):

    list_display = ('title', 'get_html_photo', 'group', 'volume', 'author', 'is_read', 'is_defer', 'is_favorites')
    fields = ('title', 'description', 'image', 'file', 'background', 'author', 'group', 'is_read', 'is_defer', 'is_favorites', 'slug')
    prepopulated_fields = {'slug': ('title',)}
    readonly_fields = ('added', 'get_html_photo')

    def get_html_photo(self, object):
        if object.image: return mark_safe(f'<img src={object.image.url} width="50px">')

    get_html_photo.short_description = 'Обложка'


@admin.register(models.Author)
class Author(admin.ModelAdmin):

    list_display = ('name', 'description', 'image')
    prepopulated_fields = {'slug': ('name',)}


@admin.register(models.Group)
class Group(admin.ModelAdmin):

    list_display = ('title', 'description', 'image')
    prepopulated_fields = {'slug': ('title',)}
